<?php
namespace Admin\Model;
use Think\Model;
class Model extends Model 
{
	protected $insertFields = array('user_id','gene_time','place','number','sex','budget','status','text','theme','end_time','age','user_type','shop_id','account_type','type','time1','hot');
	protected $updateFields = array('act_id','user_id','gene_time','place','number','sex','budget','status','text','theme','end_time','age','user_type','shop_id','account_type','type','time1','hot');
	protected $_validate = array(
		array('user_id', 'require', '用户的id不能为空！', 1, 'regex', 3),
		array('user_id', 'number', '用户的id必须是一个整数！', 1, 'regex', 3),
		array('gene_time', 'require', '开始时间不能为空！', 1, 'regex', 3),
		array('place', 'require', '活动地点不能为空！', 1, 'regex', 3),
		array('place', '1,100', '活动地点的值最长不能超过 100 个字符！', 1, 'length', 3),
		array('number', 'require', '人数不能为空！', 1, 'regex', 3),
		array('number', 'number', '人数必须是一个整数！', 1, 'regex', 3),
		array('sex', 'require', '0男1女不能为空！', 1, 'regex', 3),
		array('sex', '0,1', "0男1女的值只能是在 '0,1' 中的一个值！", 1, 'in', 3),
		array('budget', 'require', ' 预算金额不能为空！', 1, 'regex', 3),
		array('budget', 'currency', ' 预算金额必须是货币格式！', 1, 'regex', 3),
		array('status', 'require', '0即将开始1正在进行3已经结束不能为空！', 1, 'regex', 3),
		array('status', '0,1', "0即将开始1正在进行3已经结束的值只能是在 '0,1' 中的一个值！", 1, 'in', 3),
		array('text', 'require', '插入文本不能为空！', 1, 'regex', 3),
		array('theme', 'require', '主题不能为空！', 1, 'regex', 3),
		array('theme', '1,50', '主题的值最长不能超过 50 个字符！', 1, 'length', 3),
		array('age', 'require', '0未1下225下335下435不能为空！', 1, 'regex', 3),
		array('age', 'number', '0未1下225下335下435必须是一个整数！', 1, 'regex', 3),
		array('user_type', 'require', '1是商家2是用户不能为空！', 1, 'regex', 3),
		array('user_type', 'number', '1是商家2是用户必须是一个整数！', 1, 'regex', 3),
		array('shop_id', 'number', '店铺的id必须是一个整数！', 2, 'regex', 3),
		array('account_type', 'require', '结算方式不能为空！', 1, 'regex', 3),
		array('account_type', '1,50', '结算方式的值最长不能超过 50 个字符！', 1, 'length', 3),
		array('type', 'require', '1是活动2是拼单不能为空！', 1, 'regex', 3),
		array('type', 'number', '1是活动2是拼单必须是一个整数！', 1, 'regex', 3),
		array('time1', 'require', '发布的时间不能为空！', 1, 'regex', 3),
		array('hot', 'require', '是否推荐不能为空！', 1, 'regex', 3),
		array('hot', 'number', '是否推荐必须是一个整数！', 1, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($user_id = I('get.user_id'))
			$where['user_id'] = array('eq', $user_id);
		$gene_timefrom = I('get.gene_timefrom');
		$gene_timeto = I('get.gene_timeto');
		if($gene_timefrom && $gene_timeto)
			$where['gene_time'] = array('between', array(strtotime("$gene_timefrom 00:00:00"), strtotime("$gene_timeto 23:59:59")));
		elseif($gene_timefrom)
			$where['gene_time'] = array('egt', strtotime("$gene_timefrom 00:00:00"));
		elseif($gene_timeto)
			$where['gene_time'] = array('elt', strtotime("$gene_timeto 23:59:59"));
		if($place = I('get.place'))
			$where['place'] = array('like', "%$place%");
		if($number = I('get.number'))
			$where['number'] = array('eq', $number);
		$sex = I('get.sex');
		if($sex != '' && $sex != '-1')
			$where['sex'] = array('eq', $sex);
		$budgetfrom = I('get.budgetfrom');
		$budgetto = I('get.budgetto');
		if($budgetfrom && $budgetto)
			$where['budget'] = array('between', array($budgetfrom, $budgetto));
		elseif($budgetfrom)
			$where['budget'] = array('egt', $budgetfrom);
		elseif($budgetto)
			$where['budget'] = array('elt', $budgetto);
		$status = I('get.status');
		if($status != '' && $status != '-1')
			$where['status'] = array('eq', $status);
		if($text = I('get.text'))
			$where['text'] = array('eq', $text);
		if($theme = I('get.theme'))
			$where['theme'] = array('like', "%$theme%");
		$end_timefrom = I('get.end_timefrom');
		$end_timeto = I('get.end_timeto');
		if($end_timefrom && $end_timeto)
			$where['end_time'] = array('between', array(strtotime("$end_timefrom 00:00:00"), strtotime("$end_timeto 23:59:59")));
		elseif($end_timefrom)
			$where['end_time'] = array('egt', strtotime("$end_timefrom 00:00:00"));
		elseif($end_timeto)
			$where['end_time'] = array('elt', strtotime("$end_timeto 23:59:59"));
		if($age = I('get.age'))
			$where['age'] = array('eq', $age);
		if($user_type = I('get.user_type'))
			$where['user_type'] = array('eq', $user_type);
		if($shop_id = I('get.shop_id'))
			$where['shop_id'] = array('eq', $shop_id);
		if($account_type = I('get.account_type'))
			$where['account_type'] = array('like', "%$account_type%");
		if($type = I('get.type'))
			$where['type'] = array('eq', $type);
		if($time1 = I('get.time1'))
			$where['time1'] = array('eq', $time1);
		if($hot = I('get.hot'))
			$where['hot'] = array('eq', $hot);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.act_id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['picture']) && $_FILES['picture']['error'] == 0)
		{
			$ret = uploadOne('picture', 'Admin', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['picture'] = $ret['images'][0];
				$data['big_picture'] = $ret['images'][1];
				$data['mid_picture'] = $ret['images'][2];
				$data['sm_picture'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['picture']) && $_FILES['picture']['error'] == 0)
		{
			$ret = uploadOne('picture', 'Admin', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['picture'] = $ret['images'][0];
				$data['big_picture'] = $ret['images'][1];
				$data['mid_picture'] = $ret['images'][2];
				$data['sm_picture'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_picture'),
				I('post.old_big_picture'),
				I('post.old_mid_picture'),
				I('post.old_sm_picture'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['act_id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('picture,big_picture,mid_picture,sm_picture')->find($option['where']['act_id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}